/*
 * FOF上一步下一步模块
 */
var common = require('./common.js');
var user = require('./user.js');
var create = require('./create.js');
var config = require('./config.js');
var configGraph = require('./configGraph.js');
var fund = require('./fund.js');
var combination = require('./combination.js');
var combinationGraph = require('./combinationGraph.js');
var page = {
    init: function () {
        this.initEvents();
        if (user.getUser()) {
            $('#fofLeft,#fofRight').show();
        }
        window['fofPage'] = 1;
    },
    getPage: function () {
        return parseInt(window['fofPage'] || 1, 10);
    },
    setPage: function (p) {
        page.setStep(p);
        window['fofPage'] = p;
    },
    getStep: function () {
        var step = window['fofStep'];
        return step || '';
    },
    setStep: function (s) {
        if (window['fofStep']) {
            window['fofStep'] = window['fofStep'] + ',' + s;
        } else {
            window['fofStep'] = '' + s;
        }
    },
    initEvents: function () {
        $('#fofLeft').click(function () {
            var currentPage = page.getPage();
            if (currentPage > 1) {
                var step = page.getStep();
                if (step.indexOf(',') > -1) {
                    var steps = step.split(',');
                    if ($.inArray((currentPage - 1) + '', steps) > -1) {
                        page.showPage(currentPage - 1);
                    } else {
                        page.showPage(steps[steps.length - 2]);
                    }
                } else {
                    page.showPage(1);
                }
            }
        });
        $('#fofRight').click(function () {
            var currentPage = page.getPage();
            if (currentPage < 7) {
                page.showPage(currentPage + 1);
            }
        });
    },
    showPage: function (nextPage) {
        console.log('page:' + nextPage);
        var currentPage = page.getPage();
        var isNext = false;
        if (nextPage > currentPage) {
            isNext = true;
        }
        if (nextPage == 1) {
            $('.step').hide();
            common.initNav(0);
            page.setPage(nextPage);
            $('#index').show();
        } else if (nextPage == 2) {
            var createObj = $('#create');
            if (createObj.html() == '' || isNext) {
                create.init();
            } else {
                $('.step').hide();
                common.initNav(1);
                page.setPage(nextPage);
                createObj.show();
            }
        } else if (nextPage == 3) {
            var configObj = $('#config');
            if (configObj.html() == '' || isNext) {
                create.next();
            } else {
                $('.step').hide()
                common.initNav(2);
                page.setPage(nextPage);
                configObj.show();
            }
        } else if (nextPage == 4) {
            var configGraphObj = $('#configGraph');
            if (configGraphObj.html() == '' || isNext) {
                config.next();
            } else {
                $('.step').hide();
                common.initNav(2);
                page.setPage(nextPage);
                configGraphObj.show();
            }
        } else if (nextPage == 5) {
            var fundObj = $('#fund');
            if (fundObj.html() == '' || isNext) {
                configGraph.next();
            } else {
                $('.step').hide();
                common.initNav(3);
                page.setPage(nextPage);
                fundObj.show();
            }
        } else if (nextPage == 6) {
            var combinationObj = $('#combination');
            if (combinationObj.html() == '' || isNext) {
                fund.next();
            } else {
                $('.step').hide();
                common.initNav(4);
                page.setPage(nextPage);
                combinationObj.show();
            }
        } else if (nextPage == 7) {
            var combinationGraphObj = $('#combinationGraph');
            if (combinationGraphObj.html() == '' || isNext) {
                combination.next();
            } else {
                $('.step').hide();
                common.initNav(4);
                page.setPage(nextPage);
                combinationGraphObj.show();
            }
        } else if (nextPage == 8) {
            return;
            var showObj = $('#show');
            if (showObj.html() == '' || isNext) {
                combinationGraph.next();
            } else {
                $('.step').hide();
                common.initNav(5);
                page.setPage(nextPage);
                showObj.show();
            }
        }
    },
    initSelectedParams: function (page) {
        try {
            if (page == 3) {
                var html = '';
                html += 'FOF类型：' + $.trim($('input[name="FOFStyle"]:checked').parent().text()) + '<br>';
                html += '风险偏好：' + $('.profit-calculate input').val() + '%<br>';
                html += '所投基金类型：' + $.trim($('input[name="fund"]:checked').parent().text());
                $('#progressCreate').html(html);
                $('#progress').show();
            } else if (page == 4) {
                var html = '';
                html += '资产/策略 模型：' + $.trim($('input[name="modelSelect"]:checked').parent().text());
                $('#progressConfig').html(html);
            } else if (page == 5) {
                var dot = window['configGraphDot'];
                if (!dot) {
                    return;
                }
                var html = '';
                html += '资产/策略 配置 风险：' + dot.risk + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;收益：' + dot.profit;
                $('#progressConfigGraph').html(html);
            } else if (page == 6) {
                var fundList = window['fundCheckList'];
                var html = '';
                var fundNameList = '';
                $.each(fundList, function (k, v) {
                    fundNameList += v.fundNameShort + '&nbsp;&nbsp;&nbsp;';
                });
                html += '所选基金：' + fundNameList;
                $('#progressFund').html(html);
            } else if (page == 7) {
                var html = '';
                html += '组合优化 模型：' + $.trim($('input[name="combinationModelSelect"]:checked').parent().text());
                $('#progressCombination').html(html);
            } else if (page == 8) {
                var dot = window['combinationGraphDot'];
                if (!dot) {
                    return;
                }
                var html = '';
                html += '组合优化 风险：' + dot.risk + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;收益：' + dot.profit;
                $('#progressCombinationGraph').html(html);
            }
        } catch (e) {
        }
    }
};
for (var key in page) {
    if (key) {
        exports[key] = page[key];
    }
}